<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title>课工场论坛列表</title>
    <link href="css/bbs.css" rel="stylesheet">
</head>
<body>
<div class="bbs">
    <header><span onclick="openPost()">我要发帖</span></header>
    <section>
        <ul id="postList">
        </ul>
    </section>
    <div class="post" id="post">
        <input class="title" id="title" placeholder="请输入标题（1-50个字符）">
        所属版块：<select id="bankuai">
        <option>请选择版块</option>
        <option>电子书籍</option>
        <option>新课来了</option>
        <option>新手报到</option>
        <option>职业规划</option>
    </select>
        <textarea class="content" id="content"></textarea>
        <input onclick="addPost()" class="btn" value="发布">
    </div>
</div>
<script type="text/javascript">
    function openPost() {
        //显示
        document.getElementById("post").style.display = "block";
    }

    let imgs = ["tou01.jpg","tou02.jpg","tou03.jpg","tou04.jpg"];

    function addPost() {
        //发布
        //1.获取input的值
        let title = document.getElementById("title").value;//标题
        let bankuai = document.getElementById("bankuai").value;//板块
        let content = document.getElementById("content").value;//内容

        //时间
        let date = new Date();
        let strDate = date.getFullYear()+"-"+date.getMonth()+"-"+date.getDate()+" " +date.getHours()+":"+date.getMinutes();

        let n = Math.ceil(Math.random()*4)-1; //0-3 随机下标
        //2.创建节点
        let li = document.createElement("li");
        li.innerHTML = '<div><img src="images/'+imgs[n]+'"></div>\n' +
            '                <h1>'+title+'</h1>\n' +
            '                <p><span>版块：'+bankuai+'</span><span>发表时间：'+strDate+'</span></p>';

        let ul = document.getElementById("postList");
        // ul.appendChild(li);
        // 追加ul第一个空白的前面到内部前面
        ul.insertBefore(li,ul.firstChild);

        //隐藏 清空input
        document.getElementById("post").style.display = "none";
        document.getElementById("title").value = "";
    }

</script>
</body>
</html>